package com.secsbrain.cmdb.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.secsbrain.cmdb.biz.IRackService;
import com.secsbrain.sso.core.SsoHelper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.secsbrain.cmdb.biz.impl.CommonService;
import com.secsbrain.common.ResponseResult;
import com.secsbrain.cmdb.dal.model.Cabinet;
import com.secsbrain.cmdb.dal.model.Rack;
import com.secsbrain.cmdb.web.dto.CascaderOptions;

/**
 * 机架控制层
 * @author zhaoyi
 *
 */
@RestController
@RequestMapping("api/rack")
public class RackController{
	
    @Autowired
    IRackService rackService;
    
    @Autowired
    CommonService commonService;
    
    /**
     * 功能描述: <br>
     * 查询列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseResult<?> queryList(Integer page, Integer limit, Rack params) {
    	Page<Rack> result=rackService.queryPageList(page, limit, params);
    	return new ResponseResult(result);
    }
    
    /**
     * 功能描述: <br>
     * 查询机柜选项
     *
     * @return
     */
    @RequestMapping(value = "/queryRackOptions", method = RequestMethod.GET)
    public ResponseResult<?> queryRackOptions() {
    	Map<Long, String> idcMap=commonService.getIdcMap();
    	Map<Long, List<Cabinet>> cabinetListMap=commonService.getCabinetMapIdcKey();

    	Set<Map.Entry<Long, String>> idcSet=idcMap.entrySet();
    	List<CascaderOptions> idcOptionList=new ArrayList<>();
    	for (Map.Entry<Long, String> entry:idcSet) {
        	CascaderOptions idcOption=new CascaderOptions();
        	idcOption.setLabel(entry.getValue());
        	idcOption.setValue(entry.getKey());
        	
        	List<Cabinet> cabinetList=cabinetListMap.get(entry.getKey());
        	if(CollectionUtils.isEmpty(cabinetList)){
        		continue;
        	}
        	List<CascaderOptions> cabinetOptionList=new ArrayList<>();
        	for(Cabinet cabinet:cabinetList){
            	CascaderOptions cabinetOption=new CascaderOptions();
            	cabinetOption.setLabel(cabinet.getName());
            	cabinetOption.setValue(cabinet.getId());
            	cabinetOptionList.add(cabinetOption);
        	}
        	idcOption.setChildren(cabinetOptionList);
        	
        	idcOptionList.add(idcOption);
    	}
    	return new ResponseResult(idcOptionList);
    }
    
    private void valicate(Rack entity){
    	Assert.isTrue(entity.getStartu()<entity.getEndu(), "结束U必须大于起始U");
    	Assert.isTrue(entity.getCabinetId()!=null&&entity.getIdcId()!=null, "机柜不能为空");
    }
    
    /**
     * 功能描述: <br>
     * 新增
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ResponseResult<?> insert(@RequestBody Rack entity) {
    	valicate(entity);
    	
    	entity.setCreateTime(new Date());
    	entity.setCreator(SsoHelper.currentUser().getNickname());
    	rackService.insertSelective(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 更新
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public ResponseResult<?> update(@RequestBody Rack entity) {
    	valicate(entity);
    	
    	entity.setUpdateTime(new Date());
    	entity.setUpdator(SsoHelper.currentUser().getNickname());
    	rackService.updateSelectiveById(entity);
    	return new ResponseResult();
    }
    
    /**
     * 功能描述: <br>
     * 删除
     *
     * @param entity
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseResult<?> delete(@RequestBody Rack entity) {
    	rackService.deleteById(entity.getId());
    	return new ResponseResult();
    }
    
}
